package com.skycong.hello77.refresh;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.aspsine.swipetoloadlayout.SwipeLoadMoreTrigger;
import com.aspsine.swipetoloadlayout.SwipeTrigger;
import com.skycong.hello77.R;

import java.util.List;

public class LoadMoreFooterView extends RelativeLayout implements SwipeTrigger,SwipeLoadMoreTrigger{
    private ImageView ivLoadMore;

    private TextView tvLoadMOre;

    private AnimationDrawable mAnimDrawable;

    private int mHeaderHeight;
    private List<String> list;


    public LoadMoreFooterView(Context context) {
        this(context,null);
    }

    public LoadMoreFooterView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoadMoreFooterView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(getContext()).inflate(R.layout.load_more,this,true);
        mHeaderHeight = getResources().getDimensionPixelOffset(R.dimen.refresh_header_height);//得到高度

    }

    //这个方法就代表自定义控件中的子控件映射完成了，然后可以进行一些初始化控件的操作
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ivLoadMore = (ImageView) findViewById(R.id.ivloadmore);
        tvLoadMOre = (TextView) findViewById(R.id.tvloadmore);
        mAnimDrawable = (AnimationDrawable) ivLoadMore.getDrawable();
        ivLoadMore.setScaleType(ImageView.ScaleType.MATRIX);//设置图片的绘制方式，用矩阵来绘制
    }

    @Override
    public void onLoadMore() {
        if(!mAnimDrawable.isRunning()){
            mAnimDrawable.start();
        }
        tvLoadMOre.setText(R.string.load_more_ing);

    }


    @Override
    public void onPrepare() {

    }

    @Override
    public void onSwipe(int y, boolean b) {
// 这个就相当于之前的onMove了
//        接口不一样了
        if(-y>mHeaderHeight){
            tvLoadMOre.setText(R.string.load_more_release);
        }else if(-y<=mHeaderHeight){
            tvLoadMOre.setText(R.string.load_more_pull_up);
        }
    }

    @Override
    public void onRelease() {
        Log.d("LoadMore","onRelease");
        if(!mAnimDrawable.isRunning()){
            mAnimDrawable.start();
        }

    }

    @Override
    public void complete() {
         tvLoadMOre.setText(R.string.load_more_complete);

    }

    @Override
    public void onReset() {
        mAnimDrawable.stop();

    }
}
